const api = require('../../utils/api.js');
const app = getApp();

Page({
  data: {
    abnorData: {
      imgSrc: app.imgUrl + 'abnor-17.png',
      text: '商家还未发放优惠券',
    },
    unit: '',
    useTabbar: !(app.tabbarindex === 0 && app.siteInfo.tabbar_type === '1'),
    imgUrl: app.imgUrl,
    page: 1,
    is_more: true,
    coupon_list: [],
    isShowLoading: false,
    noClick: false,
  },

  onLoad(options) {
    let that = this;
    let opts = options || {};
    let isMulti =
      app.ws_micro_shop_globalData && app.ws_micro_shop_globalData.isMulti; // 是否为多店
    that.setData({ 
      opts, 
      isMulti,
      useTabbar: that.data.useTabbar && app.getTabBarStatus(),
    });
    app.pageOnLoad(this);
    app.loadCurrencyUnit().then(res => {
      that.setData({
        unit: res.unit,
      });
    });
    app.loadShopSetting().then(res => {
      if (Number(res.is_coupon) === 0) {
        wx.showModal({
          title: '友情提示',
          content: '商家暂未开启领券中心',
          showCancel: false,
          success: function(res) {
            if (res.confirm) {
              wx.navigateBack();
            }
          },
        });
        return;
      }
      that.loadData();
    });
  },
  loadData() {
    let that = this;
    let { is_more, coupon_list, page } = that.data;
    if (is_more) {
      wx.showLoading({
        title: '正在加载...',
        mask: true,
      });
      app.ws_micro_shoprequest({
        url: api.default.coupon_list,
        data: {
          page: page,
        },
        success: function(res) {
          if (res.code === 0) {
            let new_coupon_list = res.data.list || [];
            if (new_coupon_list.length > 0) {
              page += 1;
              that.setData({
                coupon_list: coupon_list.concat(new_coupon_list),
                page,
              });
            } else {
              that.setData({
                is_more: false,
              });
            }
          } else {
            wx.showToast({
              title: res.msg || '',
              icon: 'none',
            });
          }
        },
        complete: function() {
          wx.hideLoading();
          that.setData({
            isShowLoading: true,
          });
        },
      });
    }
  },
  onReachBottom() {
    this.loadData();
  },

  togetEvent1(e) {
    let _this = this;
    let id = e.detail.id;
    if (!_this.hideGetCoupon) {
      _this.hideGetCoupon = function(e) {
        let url = e.currentTarget.dataset.url || false;
        _this.setData({
          get_coupon_list: null,
        });
        if (url) {
          wx.navigateTo({
            url: url,
          });
        }
      };
    }
    app.ws_micro_shoprequest({
      url: api.coupon.receive,
      data: {
        id: id,
      },
      showLoading: true,
      success: function(res) {
        if (res.code === 0) {
          _this.setData({
            get_coupon_list: res.data.list,
            coupon_list: res.data.coupon_list,
          });
        }
        if (res.code === 1) {
          wx.showToast({
            title: res.msg,
            icon: 'none',
          });
        }
      },
    });
  },

  inputChange(e) {
    let val = e.detail.value.trim();
    this.setData({
      codeVal: val,
    })
  },

  exchange() {
    let _this = this;
    let { codeVal: code, noClick } = _this.data;
    if (!code) {
      wx.showToast({
        title: '请输入优惠券兑换码',
        icon: 'none',
      });
      return false;
    } 
    if (noClick) {
      return false;
    }
    this.setData({
      noClick: true
    })
    app.ws_micro_shoprequest({
      url: api.coupon.exchange,
      data: {
        code,
      },
      showLoading: true,
      success: function(res) {
        if (res.code === 0) {
          wx.showToast({
            title: res.msg,
            icon: 'sucess',
          });
          setTimeout(function () {
            _this.setData({
              is_more: true,
              page: 1,
              coupon_list: [],
              isShowLoading: false,
            }, () => {
              _this.loadData();
            })
          }, 1500)
        }
        if (res.code === 1) {
          wx.showToast({
            title: res.msg,
            icon: 'none',
          });
        }
        _this.setData({
          noClick: false,
        })
      },
    });
  },

  closeCouponBox(e) {
    this.setData({
      get_coupon_list: '',
    });
  },

  collectFormId(e) {
    app.collectFormId(e);
  },
});
